package com.zx.idc.backend.gui.api.management.scheduler.job;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.api.management.scheduler.base.BaseFilterAndMonitorJob;
import com.zx.idc.common.attributes.FTPReportDataTypeEnum;
import com.zx.idc.common.attributes.NetCommunicationFileTypeEnum;
import com.zx.idc.common.attributes.SchemaFileNameEnum;
import com.zx.idc.common.constants.CommonConstant;
import com.zx.idc.common.constants.InsStatusConstants;
import com.zx.idc.common.util.DigestUtil;
import com.zx.idc.ds.httplog.entity.FilterPageSnapshot;
import com.zx.idc.ds.httplog.repository.FilterPageSnapshotRepository;
import com.zx.idc.ds.ins.entity.InsFilter;
import com.zx.idc.ds.ins.entity.InsFilterArchive;
import com.zx.idc.ds.ins.service.IInsFilterArchiveService;
import com.zx.idc.ds.ins.service.IInsFilterService;
import com.zx.idc.xml.entity.common.filtermonitor.log.Log;
import com.zx.idc.xml.entity.filterresult.FilterResult;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

import static com.zx.idc.common.attributes.SystemAttributes.WEB_SERVICE_FILTER_LOG_DIR;

/**
 * 过滤指令定时上报 20分钟一次
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class ReportFilterLogDataJob extends BaseFilterAndMonitorJob<IInsFilterService, InsFilter, FilterResult> {

    private static final Logger LOG = LoggerFactory.getLogger(ReportFilterLogDataJob.class);

    private static final String COLUMN_REPORT = "report";

    @Autowired
    private IInsFilterArchiveService iInsFilterArchiveService;


    @Autowired
    private FilterPageSnapshotRepository filterPageSnapshotRepository;

    public ReportFilterLogDataJob() {
        super(
            WEB_SERVICE_FILTER_LOG_DIR,
            SchemaFileNameEnum.ILLEGAL_INFO_FILTER_RECORD_REPORT,
            FTPReportDataTypeEnum.FTP_ILLEGAL_INFORMATION_FILTERING_RECORD,
            NetCommunicationFileTypeEnum.FILTER_LOG,
            "过滤指令定时上报job");
    }


    @Override
    protected FilterResult reportData(List<Log> baseLogs, String idcId) {
        return new FilterResult()
            .setLog(baseLogs)
            .setIdcId(idcId)
            .setTimeStamp(LocalDateTime.now());
    }

    /**
     * 获取快照
     *
     * @param url
     * @return
     */
    @Override
    protected String snapshot(String url) {
        String md5Digest = DigestUtil.md5Digest(url);
        FilterPageSnapshot byUrlMd5Value = filterPageSnapshotRepository.findByUrlMd5Value(md5Digest);
        return byUrlMd5Value.getPageContent();
    }

    @Override
    protected boolean filter(String text) {
        //过滤上报字段为不上报的数据
        String[] split = text.split(CommonConstant.LOG_FIELD_SEPARATOR);
        String commandId = split[0];
        InsFilterArchive queryCondition = new InsFilterArchive();
        queryCondition.setId(commandId);
        InsFilterArchive report = iInsFilterArchiveService.selectFirst(new EntityWrapper<>(queryCondition).setSqlSelect(COLUMN_REPORT));
        LOG.debug("过滤日志上报，从过滤日志存档表查询对比是否需要上传，判定report是否为空：{}", report == null);
        if (report != null) {
            return InsStatusConstants.REPORT_STATUS_UPLOAD.equals(report.getReport());
        }
        return false;
    }
}
